package www.xpu.edu.app;
//招聘者
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Activity_job extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        final Button button=(Button)findViewById(R.id.job_button);
        final EditText edit1=(EditText)findViewById(R.id.job_text1);
        final EditText edit2=(EditText)findViewById(R.id.job_text2);
        final EditText edit3=(EditText)findViewById(R.id.job_text3);
        //取到login传来的num值
        SharedPreferences pref=getSharedPreferences("num",MODE_PRIVATE);
        final String num=pref.getString("num","");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str="您输入的内容:姓名"+edit1.getText().toString()+"公司"+edit2.getText().toString()+"职务"+edit3.getText().toString();
                System.out.println(str);
                //Toast.makeText(Activity_job.this,str,Toast.LENGTH_LONG).show();
               //跳转Activity放到子线程去执行
                sendReguest(edit1.getText().toString().trim(),edit2.getText().toString().trim(),edit3.getText().toString().trim(),num);
            }
        });



    }
    public void sendReguest(final String name, final String value1,final String value2,final String num) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                //        http://localhost:8080/xpu/reg1?num=19829088783&password=12345678

                try {
                    //局域网
                    //URL url = new URL("http://192.168.123.196:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    //http://188.131.250.4:8080
                    //URL url=new URL("http://188.131.250.4:8080/xpu/reg2?name=%E6%B5%8B%E8%AF%95&value1=%E6%B5%8B%E8%AF%95&value2=%E6%B5%8B%E8%AF%95&num=15515551555");

                    String name1 = URLEncoder.encode(name.toString().trim(), "utf-8");
                    String value1_1 = URLEncoder.encode(value1.toString().trim(), "utf-8");
                    String value2_1 = URLEncoder.encode(value2.toString().trim(), "utf-8");
                    URL url = new URL("http://188.131.250.4:8080/xpu/reg2" + "?name=" + name1 + "&value1=" + value1_1+"&value2="+value2_1+"&num="+num);
                    /*
                    通过java提供的方法(在java.net包)加密和解密
                    String mytext = java.net.URLEncoder.encode("中国", "utf-8");
                    String mytext2 = java.net.URLDecoder.decode(mytext, "utf-8");
                    输出结果:
                    mytext: %E4%B8%AD%E5%9B%BD
                    mytex2: 中国
                    */
                    System.out.println(url.toString());
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    //connection.setRequestMethod("POST");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream ips = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(ips));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    showResponse(response.toString());
                    /*if (!response.toString().trim().equals("0"))
                        startActivity(new Intent(Activity_reg.this, Activity_change.class));*/

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(response.trim().equals("添加成功"))
                {
                    Toast.makeText(Activity_job.this,"恭喜你,信息添加成功",Toast.LENGTH_LONG).show();
                    startActivity(new Intent(Activity_job.this,Activity_findjob.class));
                }
                else
                {
                    Toast.makeText(Activity_job.this,"信息添加失败，请重试或联系管理员",Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}
